package com.jun.flower.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jun.flower.bean.PageResult;
import com.jun.flower.bean.TbFlowersExample;
import com.jun.flower.bean.TbUser;
import com.jun.flower.bean.TbUserExample;
import com.jun.flower.bean.TbUserExample.Criteria;
import com.jun.flower.dao.TbUserMapper;
import com.jun.flower.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private TbUserMapper userMapper;

	@Override
	public PageResult<TbUser> getUserList(Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		List<TbUser> list = userMapper.selectByExample(null);

		PageInfo<TbUser> pageInfo = new PageInfo<>(list);
		// 取总记录数
		long total = pageInfo.getTotal();
		// 取总页数
		int pages = pageInfo.getPages();
		// 创建一个PageResult对象
		PageResult<TbUser> pageResult = new PageResult<>();
		pageResult.setList(list);
		pageResult.setPageNum(pageNum);
		pageResult.setPages(pages);
		pageResult.setPageSize(pageSize);
		pageResult.setTotal(total);
		return pageResult;
	}

	@Override
	public TbUser getUserById(Integer id) {
		TbUser user = userMapper.selectByPrimaryKey(id);
		return user;
	}

	@Override
	public boolean checkUsername(String username) {
		TbUserExample example = new TbUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<TbUser> list = userMapper.selectByExample(example);
		if (list == null || list.size() == 0) {
			// 如果没有查询到数据返回true
			return true;
		}
		// 如果查询到数据返回false
		return false;
	}

	@Override
	public void addUser(TbUser user) {
		// TODO Auto-generated method stub
		userMapper.insert(user);
	}

	@Override
	public Boolean deleteUser(String ids) {
		// 1、把字符串进行字符串拆分，可以得到一个字符串数组。
		String[] idsArr = ids.split(",");
		// 2、遍历数组把每个元素转换成Integer类型，并且放到一个list中
		List<Integer> idsList = new ArrayList<>();
		for (String string : idsArr) {
			Integer id = Integer.parseInt(string);
			idsList.add(id);
		}
		// 3、创建查询条件
		TbUserExample example = new TbUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIn(idsList);
		// 4、执行删除操作
		userMapper.deleteByExample(example);
		// 5、返回结果
		return true;
	}

	@Override
	public void updateUserById(TbUser user) {
		userMapper.updateByPrimaryKey(user);
	}

	@Override
	public TbUser loginUser(String username,String password) {
		TbUserExample example = new TbUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(username);
		criteria.andPasswordEqualTo(password);
		List<TbUser> list = userMapper.selectByExample(example);
		if (list != null & list.size() != 0) {
			// 如果查询到数据返回user
			TbUser user = list.get(0);
			return user;
		}else {
		// 如果没有查询到数据返回null
		return null;
		}
	}

	@Override
	public TbUser getUserByUserName(String username) {
		TbUserExample example = new TbUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<TbUser> list = userMapper.selectByExample(example);
		if (list != null & list.size() != 0) {
			// 如果查询到数据返回user
			TbUser user = list.get(0);
			return user;
		}else {
		// 如果没有查询到数据返回null
		return null;
		}
	}

}
